﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CosmosKey.WindowsUtils.Downr
{
    public class Config
    {
        static string REG_BASE = @"HKEY_LOCAL_MACHINE\Software\Policies\CosmosKey\Downr";
        public static int CounterSeconds {
            get
            {
                object val = Registry.GetValue(REG_BASE, "CounterSeconds", null);
                return val != null && val is Int32 ? (int)val : 3;
            }
        }

        public static int Counter
        {
            get;set;
        }
        private static TimeSpan _timeToStart = new TimeSpan();
        public static TimeSpan TimeToStart
        {
            get
            {
                if (_timeToStart == new TimeSpan())
                {
                    var defaultTime = new TimeSpan(0, 0, 0);
                    string val = (string)Registry.GetValue(REG_BASE, "TimeToStart", null);
                    val = val != null && val is string ? (string)val : "19:00:00";
                    if (!TimeSpan.TryParse(val, out defaultTime))
                    {
                        defaultTime = new TimeSpan(19, 0, 0);
                    }
                    return defaultTime;
                }
                else
                {
                    return _timeToStart;
                }
            }
            set { _timeToStart = value; }
        }
        public static string TitleString
        {
            get
            {
                object val = Registry.GetValue(REG_BASE, "TitleString", null);
                return val != null && val is string ? (string)val : "Downr";
            }
        }
        public static string CountingString
        {
            get
            {
                object val = Registry.GetValue(REG_BASE, "CountingString", null);
                return val != null && val is string ? (string)val : "The computer is shuting down in {0} seconds!";
            }
        }
        public static string ShuttingDownNowString
        {
            get
            {
                object val = Registry.GetValue(REG_BASE, "ShuttingDownNowString", null);
                return val != null && val is string ? (string)val : "The computer is shuting down now!";
            }
        }
        public static string ShuttingDownButtonString
        {
            get
            {
                object val = Registry.GetValue(REG_BASE, "ShuttingDownButtonString", null);
                return val != null && val is string ? (string)val : "Shutdown now";
            }
        }
        public static string PostponeButtonString
        {
            get
            {
                object val = Registry.GetValue(REG_BASE, "PostponeButtonString", null);
                return val != null && val is string ? (string)val : "Postpone 30 minutes";
            }
        }
        public static int PostponeTime
        {
            get
            {
                object val = Registry.GetValue(REG_BASE, "PostponeTime", null);
                return val != null && val is Int32 ? (int)val : 30;
            }
        }
       
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            
            ShutDownTimer.Interval = 1000;
            ShutDownTimer.Tick += new EventHandler(ShutDownTimer_Tick);
            ShutDownTimer.Start();
            Config.Counter = Config.CounterSeconds;

            CountDownDialog = new DownrForm();
            Application.Run();
        }

        static void ShutDownTimer_Tick(object sender, EventArgs e)
        {
            if (Config.TimeToStart < DateTime.Now.TimeOfDay && !CountDownDialog.Visible)
            {
                CountDownDialog.Show();
                CountDownDialog.StartCountingDown();
            }
        }

        static DownrForm CountDownDialog;
        static Timer ShutDownTimer = new Timer();
        static void Init()
        {
            
        }
    }
}
